<?php
namespace app\admin\controller;
use think\Db;
use app\admin\controller\Auth;
use think\Config;
class Aboutus extends Auth{
	public function aboutUsList(){
		$list = Db::name('aboutus')->where('type = 1')->paginate(10);
		$this->assign('list',$list);
		return $this->fetch();
	}
	public function addAboutUs(){
		return $this->fetch();
	}
	public function aboutUs(){
		$info = Db::name('aboutus')->where('type = 0')->value('content');
		$this->assign('info',$info);
		return $this->fetch();
	}
	public function doAddAboutUs(){
		$title = trim(input('post.title'));
		$content = trim(input('post.content'));
		if(empty($title) || empty($content)){
			$this->error('请将表单补充完整');
			exit;
		}else{
			$data['title'] = $title;
			$data['content'] = $content;
		}
		$image = request()->file('image');
		if(empty($image)){
			$this->error('请选择图片');
			exit;
		}
		$info = $image->move(ROOT_PATH . 'public' . DS . 'uploads');
		if($info){
			// 成功上传后 获取上传信息
			// 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
			$data['img_url'] = Config::get('view_replace_str.__UPLOAD__').'/'.$info->getSaveName();
		}else{
			$this->error($image->getError());
			exit;
		}
		$data['type'] = 1;
		$data['createtime'] = time();
		$result = Db::name('aboutus')->insert($data);
		if($result){
			$this->redirect('/admin/aboutus/addAboutUs');
		}else{
			$this->error('插入失败');
		}
	}
	public function delAboutUs(){
		$id = input('aid');
		if(empty($id)){
			$this->error('禁止非法操作');
			exit;
		}
		$where['id'] = array('EQ',$id);
		$result = Db::name('aboutus')->where($where)->delete();
		if($result){
			$this->redirect('/admin/aboutus/aboutUsList');
		}else{
			$this->error('删除失败');
		}
	}
	public function updateAboutUs(){
		$is_info = Db::name('aboutus')->where('type = 0')->find();
		$content = trim(input('post.content'));
		if(empty($content)){
			$this->error('详情内容不能为空');
			exit;
		}
		$data['content'] = $content;
		if(empty($is_info)){
			$data['type'] = 0;
			$data['createtime'] = time();
			$result = Db::name('aboutus')->insert($data);
		}else{
			$result = Db::name('aboutus')->where('type = 0')->update($data);
		}
		if($result){
			$this->redirect('/admin/aboutus/aboutUs');
		}else{
			$this->error('修改失败');
		}
	}
}